/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: WxSearchServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/31 14:14
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketSearchHistoryExample;
import com.cskaoyan.bean.vo.SearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class WxSearchServiceImpl implements WxSearchService{
    /*
    *在分类表内部点击搜索后的页面展示
    * */

    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper historyMapper;
    @Override
    public SearchIndexVo getSearchIndexInfoShow() {
        //需要查询三个对象 历史记录,以及热点搜索
        //查询默认搜索新信息
        //没有做搜索记录没有的时候的校验
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        //TODO
        //查询默认的搜索对象
        marketKeywordExample.createCriteria().andIsDefaultEqualTo(true);
        List<MarketKeyword> defaultKeyWordList=keywordMapper.selectByExample(marketKeywordExample);
        //创建要返回的对象
        SearchIndexVo searchIndexVo = new SearchIndexVo();
        //这里的默认的信息只能是一条 可能出现没有的情况
        if(defaultKeyWordList==null){
            //不对这个属性进行赋值
        }else {
            //不为空的话 取第一条记录  热点信息
            MarketKeyword marketKeyword = defaultKeyWordList.get(0);
            searchIndexVo.setDefaultKeyword(marketKeyword);
        }
        //查询所有的热点关键字的信息
        marketKeywordExample.clear();
        //创建查询热点关键字的对象
        marketKeywordExample.createCriteria().andIsHotEqualTo(true);
        //查询所有的人丹关键字信息
        List<MarketKeyword> hotKeyWordList=keywordMapper.selectByExample(marketKeywordExample);
        //给要返回的集合的属性赋值
        searchIndexVo.setHotKeywordList(hotKeyWordList);
        //查找历史搜索关键字记录  需要从history中获取信息
        //marketKeywordExample.clear();
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        ///根据搜索的时间进行逆序排序
        marketSearchHistoryExample.setOrderByClause("update_time desc");
        marketSearchHistoryExample.createCriteria().andDeletedEqualTo(false);
        List<MarketSearchHistory> historyKeyWordList=historyMapper.selectByExample(marketSearchHistoryExample);
        //创建要返回的对象属性的historyKeyWordList集合
        LinkedList<MarketSearchHistory> historyBo = new LinkedList<>();
        for (MarketSearchHistory marketKeyword : historyKeyWordList) {
            MarketSearchHistory historyKeyword = new MarketSearchHistory();
            historyKeyword.setKeyword(marketKeyword.getKeyword());
            historyBo.add(historyKeyword);
        }
        //给要返回对象的historyKeyword属性赋值
        searchIndexVo.setHistoryKeywordList(historyBo);
        return searchIndexVo;
    }
/*
* 清除所有的记录  修改状态码 0====>1
* */
    @Override
    public void clearHistory() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        //穿创建要修改的条件
        marketSearchHistory.setDeleted(true);
        //修改语句
        historyMapper.updateByExampleSelective(marketSearchHistory,marketSearchHistoryExample);
    }
/*
* 查询帮助
* */
    @Override
    public String[] searchHelper(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andKeywordLike("%"+keyword+"%");
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(marketKeywordExample);
        int size = marketKeywords.size();
        String[] str = new String[size];
        int index=0;
        for (MarketKeyword marketKeyword : marketKeywords) {
            str[index]=marketKeyword.getKeyword();
            index++;
        }
        return str;
    }
}
